package com.tstudy.laoshibang.custom;

import android.content.Context;
import android.graphics.Color;
import android.text.Layout;
import android.text.TextPaint;
import android.view.Gravity;
import android.widget.TextView;

/**
 * 自定义textview，根据字数确定width
 * @author wuyahui
 */
public class CustomTextView extends TextView {
	private int width;
	public CustomTextView(Context context) {
		super(context);
		setSingleLine(true);
		setGravity(Gravity.CENTER);
		setTextColor(Color.WHITE);
//		setShadowLayer(2, 0, -1, Color.argb(0x80, 0x00, 0x00, 0x00));
	}

	/**
	 * 只有计算宽度的时候才用到
	 * 
	 * @param text
	 */
	public void setCustomText(CharSequence text,int padding) {
		TextPaint paint = this.getPaint();
		this.width = (int) Layout.getDesiredWidth(text, paint) + padding;
		if (text == null || text.length() <= 0) {
			this.width = 0;
		}
		this.setText(text);
	}

	/**
	 * 获得该View的显示宽度 (px)
	 * 
	 * @return int
	 */
	public int getViewWidth() {
		return width;
	}

	
}
